package com.kinyx.framework.security;

import java.io.File;
import java.io.UnsupportedEncodingException;

import org.junit.Assert;
import org.junit.Test;

import com.kinyx.framework.constraints.BASE64;
import com.kinyx.framework.file.KFileReader;
import com.kinyx.framework.file.KPathUtils;
import com.kinyx.framework.utils.KMatcherUtils;

public class KBASE64_Tester_ {

	@Test
	public void test() throws UnsupportedEncodingException {
		final String fileName = "/file/category/test.jpg";
		final File file = new File(KPathUtils.resources(true) + fileName);

		final byte[] bytes1 = KFileReader.readBytes(fileName);
		final byte[] bytes2 = KFileReader.readBytes(file);

		Assert.assertEquals(bytes1.length, bytes2.length);
		for (int i = 0; i < bytes1.length; i++) {
			Assert.assertEquals(bytes1[i], bytes2[i]);
		}

		final String encode1 = KBASE64.encode(bytes1, null);
		final String encode2 = KBASE64.encode(bytes2, null);
		Assert.assertEquals(encode1, encode2);
		Assert.assertTrue(KMatcherUtils.matches(encode1, BASE64.REGEX));

		final byte[] decode = KBASE64.decode(encode1);

		Assert.assertEquals(decode.length, bytes1.length);
		for (int i = 0; i < decode.length; i++) {
			Assert.assertEquals(decode[i], bytes1[i]);
		}
	}

}
